package com.dci.controller.management;


import com.dci.model.management.ProjectManageInfo;
import com.dci.model.management.TaskInfo;
import com.dci.result.JsonData;
import com.dci.result.PageResult;
import com.dci.service.management.ProjectManageService;
import com.dci.service.management.TaskInfoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.controller.management
 * @Description: 项目管理库
 * @author: 张星亮
 * @date: 2019/2/25
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Controller
@RequestMapping("/manage")
public class ProjectManageController {


    @Resource
    private ProjectManageService projectManageService;

    @Resource
    private TaskInfoService taskInfoService;

    @Value("${upload.path}")
    private String path;
    @Value("${upload.urlP}")
    private String urlP;

    /**
     * 项目管理库, 新增项目申报,
     *
     * @param record
     * @return JsonData 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @RequestMapping("/save")
    @ResponseBody
    public JsonData saveProjectManageInfo(@RequestBody ProjectManageInfo record) {
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String address = df.format(now);
        String filePath = urlP + address + "/";
        record.setFile_url(filePath);
        record.setProject_progess("立项");
        try {
            return projectManageService.saveProjectManageInfo(record);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonData.success(null, 911);
        }
    }


    /**
     * 项目管理库, 获取项目信息
     *
     * @param record
     * @return JsonData 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @RequestMapping("/get")
    @ResponseBody
    public JsonData getProjectManageInfo(@RequestBody ProjectManageInfo record) {
        try {
            return projectManageService.getProjectManageInfo(record);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonData.success(null, 911);
        }
    }

    /**
     * 审核改变提交状态
     *
     * @param id
     * @return JsonData 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @RequestMapping("/updateSub")
    @ResponseBody
    public JsonData updateSub(@RequestParam("id") String id, @RequestParam("progess_number") String number) {
        ProjectManageInfo record = new ProjectManageInfo();
        record.setId(id);
        record.setSub_status("0");
        int progess = Integer.valueOf(number);
        if (progess < 5) {
            progess = progess + 1;
        }
        ;
        record.setProgess_number(progess);
        try {
            return projectManageService.batchUpdateManageInfo(record);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonData.success(null, 911);
        }
    }

    /**
     * 古保资金项目, 更新数据
     *
     * @param number
     * @param id
     * @return JsonData 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @RequestMapping("/update")
    @ResponseBody
    public JsonData updateProjectManageInfo(@RequestParam("uploadFile") MultipartFile[] files, @RequestParam("id") String id, @RequestParam(value = "progess_number", required = false) String number, @RequestParam(value = "file_url", required = false) String file_url, @RequestParam("sub_status") String sub_status) {
        ProjectManageInfo record = new ProjectManageInfo();
        record.setFile_url(file_url);
        record.setId(id);
        if (number != null) {
            record.setProgess_number(Integer.valueOf(number));
        } else {
            record.setProgess_number(0);
        }
        if (record.getFile_url() == null && record.getProgess_number() == 0) {
            Date now = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            String address = df.format(now);
            String filePath = path + File.separator + address + "/";
            String fileNames = "";
            if (files.length != 0) {
                for (MultipartFile file : files) {
                    String fileName = file.getOriginalFilename();
                    if (fileNames == "") {
                        fileNames += fileName;
                    } else {
                        fileNames += ',' + fileName;
                    }
                    File fileDir = new File(filePath);
                    if (!fileDir.exists()) {
                        fileDir.mkdirs();
                    }
                    try {
                        file.transferTo(new File(fileDir.getAbsolutePath(), fileName));
                    } catch (IOException e) {
                        continue;
                    }
                }
            }
            String urlPath = urlP + address + "/";
            record.setFile_url(urlPath);
            record.setApproval_file(fileNames);
        } else {
            String fileNames = "";
            String url = record.getFile_url();
            String result = "";
            int i = 0;
            while (i < 2) {
                int lastFirst = url.lastIndexOf('/');
                result = url.substring(lastFirst) + result;
                url = url.substring(0, lastFirst);
                i++;
            }
            String P = result.substring(1);
            String filePath = path + "/" + P;
            if (files.length != 0) {
                for (MultipartFile file : files) {
                    String fileName = file.getOriginalFilename();
                    if (fileNames == "") {
                        fileNames += fileName;
                    } else {
                        fileNames += ',' + fileName;
                    }
                    File fileDir = new File(filePath);
                    if (!fileDir.exists()) {
                        fileDir.mkdirs();
                    }
                    try {
                        file.transferTo(new File(fileDir.getAbsolutePath(), fileName));
                    } catch (IOException e) {
                        continue;
                    }
                }
            }
            int a = record.getProgess_number() + 1;
            switch (a) {
                case 1:
                    if (sub_status.equals("1")) {
                        record.setProgess_number(1);
                        record.setProject_progess("招标");
                    }
                    record.setApproval_file(fileNames);
                    break;
                case 2:
                    if (sub_status.equals("1")) {
                        record.setProgess_number(2);
                        record.setProject_progess("施工");
                    }
                    record.setTend_file(fileNames);
                    break;
                case 3:
                    if (sub_status.equals("1")) {
                        record.setProgess_number(3);
                        record.setProject_progess("验收");
                    }
                    record.setConstruction_file(fileNames);
                    break;
                case 4:
                    if (sub_status.equals("1")) {
                        record.setProgess_number(4);
                        record.setProject_progess("审计");
                    }
                    record.setCheck_file(fileNames);
                    break;
                case 5:
                    if (sub_status.equals("1")) {
                        record.setProgess_number(5);
                        record.setProject_progess("审计");
                    }
                    record.setAudit_file(fileNames);
                    break;
                default:
                    if (sub_status.equals("1")) {
                        record.setProgess_number(5);
                    }
                    break;
            }
        }
        try {
            if (sub_status.equals("0")) {
                record.setSub_status("1");
            } else {
                record.setSub_status("0");
            }
            return projectManageService.batchUpdateManageInfo(record);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonData.success(null, 911);
        }
    }


    /**
     * 非古保资金项目, 更新数据
     *
     * @param record
     * @return JsonData 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @RequestMapping("/updateFei")
    @ResponseBody
    public JsonData updateProjectManageInfoFei(@RequestBody ProjectManageInfo record) {
        try {
            return projectManageService.batchUpdateManageInfoFei(record);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonData.success(null, 911);
        }
    }


    /**
     * 古保资金项目, 查询列表
     *
     * @param record
     * @return PageResult 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @RequestMapping("/getPages")
    @ResponseBody
    public PageResult selectProjectManageList(@RequestBody ProjectManageInfo record) {
        try {
            return projectManageService.selectProjectManageList(record);
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.fail(911);
        }
    }


    /**
     * 查询任务管理列表
     *
     * @param record
     * @return
     */
    @RequestMapping("/getTaskList")
    @ResponseBody
    public JsonData selectPageList(@RequestBody TaskInfo record) {
        try {
            return taskInfoService.selectPageList(record);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonData.success(null, 911);
        }
    }

    /**
     * 查询任务管理列表
     *
     * @return
     */
    @RequestMapping("/getAll")
    @ResponseBody
    public PageResult selectPageList(@RequestParam(value = "name", required = false) String form) {
        try {
            return projectManageService.selectAll(form);
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.fail(911);
        }
    }

    /**
     * 查询任务管理列表
     *
     * @return
     */
    @RequestMapping("/getAllEnd")
    @ResponseBody
    public PageResult selectPageListEnd(@RequestParam(value = "name", required = false) String form) {
        try {
            return projectManageService.selectAllEnd(form);
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.fail(911);
        }
    }

    @RequestMapping("/saveOtherField")
    @ResponseBody
    public JsonData saveOtherField(@RequestParam(value = "fields", required = false) String fields) {
        try {
            return projectManageService.saveOtherField(fields);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonData.success(null, 911);
        }
    }

    @RequestMapping("/updateOtherField")
    @ResponseBody
    public JsonData updateOtherField(@RequestParam(value = "oldFields", required = false) String oldFields, @RequestParam(value = "newFields", required = false) String newFields) {
        try {
            return projectManageService.updateOtherField(oldFields, newFields);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonData.success(null, 911);
        }
    }

    @RequestMapping("/queryOtherField")
    @ResponseBody
    public JsonData queryOtherField() {
        try {
            return projectManageService.queryOtherField();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonData.success(null, 911);
        }
    }

}
